package work9;

import java.util.*;

class Book
{
    String ISBN, name;

    Book(String ISBN, String name)
    {
        this.ISBN = ISBN;
        this.name = name;
    }
}

public class t4 {
    public static void main(String[] args) {
        HashMap<String, Book> mapArr = new HashMap();
        Book[] book = new Book[2];
        System.out.println("输入4本书名号和书名(先输入书号回车再输入书名~重复4次)：");
        Scanner scanner = new Scanner(System.in);
        String name,sn;
        // 初始化4个学生，并将他们添加进ArrayList
        for (int i=0;i<4;i++){
            sn = scanner.nextLine();
            name = scanner.nextLine();
            book[i] = new Book(sn, name);
            mapArr.put(sn, book[i]);
        }
        System.out.println("书店中有" + mapArr.size() + "本书: ");
        Collection<Book> collection = mapArr.values();//读取全部书名
        Iterator<Book> iter = collection.iterator();//获取迭代器
        while(iter.hasNext())
        {
            Book bk = iter.next();
            System.out.println("ISBN:" + bk.ISBN + " 书名：《" + bk.name + "》");
        }
        System.out.println("输入ISBN查找书名：");
        String key = scanner.nextLine();
        if(mapArr.containsKey(key))
        {
            System.out.println("《" + mapArr.get(key).name + "》有货");// 输出其书名
//
            Book k = mapArr.remove(key); // 从HashMap中移除该本书,并返回这本书信息给k
//
            System.out.println("卖出《" + k.name + "》这本书后，书店还有" + mapArr.size() + "本书");
//
//            // 遍历输出所有元素的ISBN和书名
            collection = mapArr.values();
            iter = collection.iterator();
            while(iter.hasNext())
            {
                Book bk = iter.next();
                System.out.println("ISBN:" + bk.ISBN + " 书名：《" + bk.name + "》");
            }

        }
        else System.out.println("书店中找不到ISBN为" + key + "的书！");
        System.out.println("\n0501刘冰");
        scanner.close();
    }
}
